using QAssetBundle;
using UnityEngine;
using QFramework;

namespace ProjectSurvivor
{
	public partial class MiniBoss : ViewController, IEnemy
	{
		public enum States
		{
			FlowingPlayer, // 跟随主角
			Warning, // 警戒状态
			Dash, // 冲向主角
			Wait // 等待
		}

		public FSM<States> Fsm = new FSM<States>(); // 状态机
		
		public float HP = 50f;
		public float MovementSpeed = 2f;
		public Color DissolveColor = Color.yellow;
		private void Start()
		{
			EnemyGenerator.EnemyCount.Value++;
			
			// 添加状态
			Fsm.State(States.FlowingPlayer)
				.OnFixedUpdate(() =>
				{
					if (Player.Default)
					{
						Vector3 direction = (Player.Default.transform.position - transform.position).normalized;
						SelfRigidbody2D.velocity = direction * MovementSpeed;

						if ((Player.Default.Position() - transform.Position()).magnitude <= 15)
						{
							Fsm.ChangeState(States.Warning);
						}
					}
					else
					{
						SelfRigidbody2D.velocity = Vector2.zero;
					}
				});
			Fsm.State(States.Warning)
				.OnEnter(() =>
				{
					SelfRigidbody2D.velocity = Vector2.zero;
				})
				.OnUpdate(() =>
				{
					long frames = 3 + (60 * 3 - Fsm.FrameCountOfCurrentState) / 10;

					if (Fsm.FrameCountOfCurrentState / frames % 2 == 0)
					{
						Sprite.color = Color.red;
					}
					else
					{
						Sprite.color = Color.white;
					}
					
					if (Fsm.FrameCountOfCurrentState >= 60 * 3)
					{
						Fsm.ChangeState(States.Dash);
					}
				})
				.OnExit(() =>
				{
					Sprite.color = Color.white;
				});

			Vector3 dashStartPos = Vector3.zero;
			float dashStartDistanceToPlayer = 0f;
			
			Fsm.State(States.Dash)
				.OnEnter(() =>
				{
					dashStartPos = transform.position;
					dashStartDistanceToPlayer = (Player.Default.transform.position - transform.position).magnitude;
					Vector3 direction = (Player.Default.transform.position - transform.position).normalized;
					SelfRigidbody2D.velocity = direction * 15;
				})
				.OnUpdate(() =>
				{
					float distance =  (dashStartPos - transform.position).magnitude;
					if (distance >= dashStartDistanceToPlayer * 1.5f)
					{
						Fsm.ChangeState(States.Wait);
					}
				});;
			
			Fsm.State(States.Wait)
				.OnEnter(() =>
				{
					SelfRigidbody2D.velocity = Vector2.zero;
				})
				.OnUpdate(() =>
				{
					if (Fsm.FrameCountOfCurrentState >= 30)
					{
						Fsm.ChangeState(States.FlowingPlayer);
					}
				});
			
			// 开始状态
			Fsm.StartState(States.FlowingPlayer);
		}

		private void FixedUpdate()
		{
			Fsm.FixedUpdate();
		}

		private void Update()
		{
			Fsm.Update();
			if (HP <= 0)
			{
				Global.GeneratePowerUp(gameObject, true);
				FXController.Play(Sprite, DissolveColor);
				this.DestroyGameObjGracefully();
			}
		}

		private void OnDestroy()
		{
			EnemyGenerator.EnemyCount.Value--;
		}

		public void Hurt(float value, bool critical = false)
		{
			Sprite.color = Color.red;
			AudioKit.PlaySound(Sfx.HIT);
			FloatingTextController.Play(transform.position, value.ToString("0"), critical);
			ActionKit.Delay(0.2f, () =>
			{
				HP -= value;
				Sprite.color = Color.white;
			}).Start(this);
		}
		
		public void SetHPScale(float hpScale)
		{
			HP *= hpScale;
		}

		public void SetSpeedScale(float speedScale)
		{
			MovementSpeed *= speedScale;
		}
	}
}
